package com.approval.approvalapp.knowledge.wj;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WjBean;
import com.approval.approvalapp.knowledge.wj.adapter.WjRvAdapter;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.base.BaseFragment;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.DownResponseInterface;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.Bind;

/**
 * A simple {@link Fragment} subclass.
 */
public class WjFragment extends BaseFragment implements WjRvAdapter.ClickItemListener {
    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    private  int pagenum = 1;
    private WjRvAdapter wjRvAdapter;
    private List<WjBean> wjBeans;
    private String type;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wj;
    }

    @Override
    public void initView(View view) {
        Bundle arguments = getArguments();
        type = arguments.getString("type");

        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        wjRvAdapter = new WjRvAdapter();
        xrv.setAdapter(wjRvAdapter);

        swipeRefreshLayout.setRefreshing(true);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        },500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                xrv.setNoMore(true);
            }
        });
    }


    private void refreshData() {
        pagenum = 1;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", type);
        jsonObject.toString();
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/file_list",
                jsonObject.toString(),new NetDownResponse() {
            @Override
            public void success(String str) {
                wjBeans = JSON.parseArray(str, WjBean.class);
                wjRvAdapter.setWjBeans(wjBeans,WjFragment.this);
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public void click(int index) {
        final ProgressDialog  progressDialog = new ProgressDialog(this.getActivity());
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setProgressNumberFormat(" ");
        progressDialog.setTitle("正在下载");
        progressDialog.setMessage("请稍候...");
        progressDialog.setProgress(0);
        progressDialog.setMax(100);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();

        final String url =  wjBeans.get(index).getDF_URL();

        Log.d("WjFragmentsss", url.substring((url.lastIndexOf("/") + 1), url.length()));
        NetRequsetUtil.getInstance().downLoadFile(this.getActivity(),
                wjBeans.get(index).getDF_URL(),
                url.substring((url.lastIndexOf("/") + 1), url.length()),
                new DownResponseInterface() {
                    @Override
                    public void successFinish() {

                        progressDialog.dismiss();
                    }

                    @Override
                    public void progress(int progress) {

                        Log.d("WjFragprogress", "progress:" + progress);

                        progressDialog.setProgress(progress);
                    }

                    @Override
                    public void erro() {
                        progressDialog.dismiss();

                    }
                }
        );
    }
}
